package com.hsgene.order.controller.peanut;

import com.hsgene.order.domain.disease.GeneOrder;
import com.hsgene.order.exception.OrderErrorCode;
import com.hsgene.order.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * @description:
 * @projectName: hdas-geneshop-server
 * @package: com.hsgene.order.controller.peanut
 * @author: maodi
 * @createDate: 2018/12/26 14:18
 * @version: 1.0
 * Copyright: Copyright (c) 2018
 */
@Controller
@RequestMapping(value = "/v1.0/genetic/testing/orders/patient/confirm/page")
public class PatientInfoConfirmPageController {

    private static Logger LOGGER = LogManager.getLogger(PatientInfoConfirmPageController.class);

    @Resource
    OrderService orderService;

    /**
     * @param orderNo 订单编号
     * @param openId
     * @param model
     * @return java.lang.String
     * @description 获取患者信息确认页面
     * @author maodi
     * @createDate 2018/10/25 14:03
     */
    @GetMapping
    public String getPatientInfoConfirmPage(@RequestParam("order_no") String orderNo, @RequestParam("open_id") String
            openId, Model model) {
        try {
            if (StringUtils.isBlank(orderNo)) {
                return OrderErrorCode.PATIENT_INFO_CONFIRM_ORDER_NO_IS_NULL.getDescription();
            }
            if (StringUtils.isBlank(openId)) {
                return OrderErrorCode.PATIENT_INFO_CONFIRM_OPEN_ID_ISNULL.getDescription();
            }
            GeneOrder geneOrder = orderService.getPatientInfoConfirmByOrderNo(orderNo);
            if (geneOrder == null) {
                return OrderErrorCode.PATIENT_INFO_CONFIRM_NOT_FOUND_INFO.getDescription();
            }
            String testItems = orderService.getTestItemsByOrderNo(orderNo);
            model.addAttribute("openId", openId);
            model.addAttribute("testItems", testItems == null ? "" : testItems);
            model.addAttribute("geneOrder", geneOrder);
            return "confirm";
        } catch (Exception e) {
            LOGGER.error("获取患者信息确认页面出错", e);
            return OrderErrorCode.PATIENT_INFO_CONFIRM_PAGE_IS_ERROR.getDescription();
        }
    }

}
